<template>
<div class="add-project-wrapper">
    <Modal title="修改密码"
        v-model="modalShow"
        @on-ok="reset"
        @on-cancel="cancel"
        :auto-close="false"
        :mask-closable="false">
        <Form ref="form" :model="resetModel" :grid="1" :label-width="100" label-position="right" :rules="validateRules">
            <Form-item label="旧密码" prop="passWord">
                <Input type="password" placeholder="旧密码" clearable class="normal-input" v-model="resetModel.passWord"></Input>
            </Form-item>
            <Form-item label="新密码" prop="newPassWord">
                <Input type="password" placeholder="新密码" clearable class="normal-input" v-model="resetModel.newPassWord"></Input>
            </Form-item>
            <Form-item label="确认新密码" prop="confirmPassword">
                <Input type="password" placeholder="确认新密码" clearable class="normal-input" v-model="resetModel.confirmPassword"></Input>
            </Form-item>
        </Form>
    </Modal>
</div>
</template>

<script>
import util         from 'util'
import getData      from 'getData'
import hex_md5      from '../../tools/md5.js'

export default {

    data () {
        return {
            resetModel: {
                passWord: '',
                newPassWord: '',
                confirmPassword: ''
            },
            modalShow: false,
            validateRules: {
                passWord: [{
                    required: true,
                    message: "请输入旧密码！"
                }],
                newPassWord: [{
                    required: true,
                    message: "请输入新密码！"
                }],
                confirmPassword: [{
                    required: true,
                    message: "请输入确认新密码！"
                },
                {
                    validator: (rule, value, callback) => {
                        if (this.resetModel.newPassWord && this.resetModel.newPassWord != ''
                        && this.resetModel.confirmPassword && this.resetModel.confirmPassword != '') {
                            if (this.resetModel.newPassWord !== this.resetModel.confirmPassword) {
                                callback(new Error('确认密码与新密码不一致！'));
                            }
                            callback();
                        }
                        callback();
                    },
                    trigger: 'blur'
                }]
            },
            btnLoading: false,
        }
    },
    methods: {
        show() {
            this.modalShow = true;
        },
        reset() {
            this.$refs.form.validate(valid => {
                if (valid) {
                    var reqData = {};
                    reqData.passWord = hex_md5(this.resetModel.passWord);
                    reqData.newPassWord = hex_md5(this.resetModel.newPassWord);
                    reqData.confirmPassword = hex_md5(this.resetModel.confirmPassword);
                    getData.post('resetPassword', reqData).then(res => {
                        if (res.status == 200 && res.data.code == '000') {
                            this.$Message.success('重置密码成功！');
                            this.cancel();
                        } else if (res.status == 200) {
                            this.$Message.warning('重置密码失败: ' + res.data.errorMsg);
                        }
                    })
                }
            })
        },
        cancel() {
            this.$refs.form.resetFields();
            this.modalShow = false;
        },
    }
}
</script>
